/**
 * Copyright (c) 2007-2008 Nearchos Paspallis
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
/**
 * AspectSense
 *
 * Middleware for annotation-based, context-aware and adaptive services for
 * mobile and ubiquitous computing
 *
 * This project is an open-source effort, initiated by Nearchos Paspallis as
 * part of his PhD work at the University of Cyprus (http://www.ucy.ac.cy).
 *
 * More information about the project is available at "http://aspectsense.org".
 */

package org.aspectsense.context;

import org.aspectsense.context.events.ContextChangedEvent;
import org.aspectsense.coreservices.ContextService;
import org.aspectsense.framework.CoreServices;

import java.util.*;

/**
 * @author Nearchos Paspallis [nearchos@cs.ucy.ac.cy]
 *         Date: Jan 11, 2008
 *         Time: 11:36:50 AM
 * todo remove!
 */
public class DummyContextSimulator implements Runnable
{
    private Object [] initialization = {
            "context.user.handsfree-required", false,
            "context.infrastructure.memory-availability", 0.9,
            "context.infrastructure.cpu-availability", 0.7
    };

    // format: milliseconds to wait, context type, new value
    private Object [] trace = {
            30000L, "context.user.handsfree-required", false,
            1000L, "context.infrastructure.memory-availability", 0.1,
            5000L, "context.user.handsfree-required", true,
            5000L, "context.user.handsfree-required", false,
//            1000L, "context.infrastructure.memory-availability", 0.8,
//            1000L, "context.user.handsfree-required", true
    };

    public DummyContextSimulator()
    {
        super();

        new Thread(this).start();
    }

    public void run()
    {
        final ContextService contextService = CoreServices.getContextService();

        try
        {
            // initialization
            for(int i = 0; i < initialization.length / 2; i++)
            {
                final String contextTypeS = (String) initialization[i*2];
                final Object value = initialization[i*2 + 1];

                final ContextChangedEvent event;
                final ContextType contextType = DefaultContextType.getContextType(contextTypeS);

System.out.println("Initializing context type " + contextTypeS + " with value " + value);

                final Query query = new DefaultQuery(QueryType.CONTEXT_UPDATE, contextType);
                final Map<ContextType, ContextValue> results = new HashMap<ContextType, ContextValue>();

                if(value instanceof Double)
                {
                    results.put(contextType, new DefaultDoubleContextValue((Double) value));
                    final QueryResults queryResults = new QueryResults(results);
                    event = new ContextChangedEvent(
                            this,
                            query,
                            queryResults);
                }
                else if(value instanceof Boolean)
                {
                    results.put(contextType, new DefaultBooleanContextValue((Boolean) value));
                    final QueryResults queryResults = new QueryResults(results);
                    event = new ContextChangedEvent(
                            this,
                            query,
                            queryResults);
                }
                else
                {
                    throw new RuntimeException("Illegal context value: " + value);
                }

                contextService.raiseEvent(event);
            }

            // simulation events
            for(int i = 0; i < trace.length / 3; i++)
            {
                final long waitPeriod = (Long) trace[i*3];
                final String contextTypeS = (String) trace[i*3 + 1];
                final Object value = trace[i*3 + 2];

                Thread.sleep(waitPeriod);

System.out.println("Generating event of type " + contextTypeS + " with value " + value);

                final ContextChangedEvent event;

                final ContextType contextType = DefaultContextType.getContextType(contextTypeS);

                final Query query = new DefaultQuery(QueryType.CONTEXT_UPDATE, contextType);
                final Map<ContextType, ContextValue> results = new HashMap<ContextType, ContextValue>();

                if(value instanceof Double)
                {
                    results.put(contextType, new DefaultDoubleContextValue((Double) value));
                    final QueryResults queryResults = new QueryResults(results);
                    event = new ContextChangedEvent(
                            this,
                            query,
                            queryResults);
                }
                else if(value instanceof Boolean)
                {
                    results.put(contextType, new DefaultBooleanContextValue((Boolean) value));
                    final QueryResults queryResults = new QueryResults(results);
                    event = new ContextChangedEvent(
                            this,
                            query,
                            queryResults);
                }
                else
                {
                    throw new RuntimeException("Illegal context value: " + value);
                }

                contextService.raiseEvent(event);
            }
        }
        catch (InterruptedException ie)
        {
            System.out.println("IE: " + ie.getMessage());
        }
    }

    public String toString()
    {
        return "DummyContextSimulator";
    }
}